Skip to content

KTIJ-26211: IDEA: On selecting it highlight the call accepting it's in lambda #3472

Open
kshulzh wants to merge 1 commit intoJetBrains:masterfrom
kshulzh:KTIJ-26211
Open

KTIJ-26211: IDEA: On selecting it highlight the call accepting it's in lambda #3472
kshulzh wants to merge 1 commit intoJetBrains:masterfrom
kshulzh:KTIJ-26211

Conversation

@kshulzh
Copy link
Copy Markdown

@kshulzh kshulzh commented Mar 29, 2026

The suggestion is to highlight effective function call with caret on it.
Here my first contribute I tried to follow your instructions
task
Screenshot 2026-03-29 223744
Screenshot 2026-03-29 223759
Screenshot 2026-03-29 223856
Screenshot 2026-03-29 224238


Note

Medium Risk
Touches editor highlight-usages behavior and adds extra PSI walking and analysis checks, which could affect highlighting correctness/performance in nested-lambda edge cases.

Overview
When the caret is on an implicit it, the highlight-usages handler now also highlights the call expression that provides the lambda argument (in addition to the it occurrences), and avoids incorrectly highlighting through nested lambdas where it is shadowed or reintroduced.

This is implemented by extending implicit-lambda utilities to detect implicit-it lambdas from PSI (getLambdaFunctionLiteralByNameReference, getImplicitItLambdaParameterSymbol), updating KotlinHighlightImplicitItHandlerFactory to use the new detection and to add the call-site occurrence, registering the handler for K2, and adding shared K1/K2 test coverage plus test-generator wiring and new test data under testData/implicitIt.

Reviewed by Cursor Bugbot for commit b520e15. Bugbot is set up for automated code reviews on this repo. Configure here.

@BartvHelvert BartvHelvert added the Kotlin Pull requests for Kotlin IntelliJ plugin label Mar 31, 2026
@vladimirdolzhenko
Copy link
Copy Markdown
Contributor

Thanks a lot for the contribution 🙏

While task describe the intention, it does not describe all requirements.

Consistency and easy way for observability

When you place a caret to the { of lambda - it highlights all so-called exit points of that lambda - like explicit returns and the last effective statement

image

When you place a caret to any of that expressions, e.g.
image

corresponding { of lambda + all relevant returns point are highlighted.

Similar behaviour you can observe with fun, for/while loops.

Therefore, I would like to have expected consistency in behaviour. In the same time it looks like we could start highlighting too much - that's another topic where we have IntelliJ platform limitations - there are only two types of such highlighting - for read and for write.

Tests

Tests are crucial part of development. I would recommend to take a look into this commit e0471e0 as inspiration point how to make tests.

In short: we use so-called test-data to describe our functional requirements.
Simple example: https://github.com/JetBrains/intellij-community/blob/master/plugins/kotlin/idea/tests/testData/exitPoints/funKeyword.kt

For exit points we use ~ symbol for the caret position, <info> tags to describe what is highlighted.

@kshulzh
Copy link
Copy Markdown
Author

kshulzh commented Apr 6, 2026

While task describe the intention, it does not describe all requirements.

Hello @vladimirdolzhenko, What can I do?

  1. Change name highlight on lbrace, rbrace
  2. Add tests
  3. Something else in scope of this PR?

@vladimirdolzhenko
Copy link
Copy Markdown
Contributor

Hey @kshulzh ,

  1. TBH I don't fully understand what do you mean by change name highlight - my point: when the caret is on it highlight lambda's name - you have to highlight corresponding its when the caret at lambda's name
  2. Definitely tests / test data should be added

I think that's enough. Thanks a lot

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b520e15. Configure here.



fun KtNameReferenceExpression.getLambdaFunctionLiteralByNameReference(): KtFunctionLiteral? {
val lambda = ((this.parent as? KtCallExpression)?.lastChild as? KtLambdaArgument)?.getLambdaExpression() ?: return null
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fragile lastChild instead of idiomatic lambdaArguments accessor

Medium Severity

getLambdaFunctionLiteralByNameReference uses lastChild as? KtLambdaArgument to locate the trailing lambda argument of a KtCallExpression. PsiElement.getLastChild() returns the literal last child node in the PSI tree, which can include whitespace or error elements. The rest of the codebase consistently uses the type-safe KtCallExpression.lambdaArguments property for this purpose (e.g., in symbolUtils.kt, KotlinExpressionMover.kt, ExpectedExpressionMatcher.kt). Using lambdaArguments.singleOrNull() would be more robust and consistent with existing patterns.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b520e15. Configure here.

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 9, 2026

You have used all of your free Bugbot PR reviews.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@kshulzh
Copy link
Copy Markdown
Author

kshulzh commented Apr 9, 2026

Hello @vladimirdolzhenko, I tried to add tests and highlight for implicit "its" in case when cursor on lambda`s name
Screenshot 2026-04-09 113533
Also want to clarify some questions regarding this case. Is it valid or It is should not be hightlighted(cursor on "it")?
Screenshot 2026-04-09 121508

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Kotlin Pull requests for Kotlin IntelliJ plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants